﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Database
{
    public class Login
    {
        private List<User> users;
        public Login()
        { 
            users.Add(new User("srv-01","joyce",CalculateMD5Hash("frieten"), "euroscan"));
            users.Add(new User("srv-01", "jos", CalculateMD5Hash("buisness"), "euroscan"));
            users.Add(new User("srv-02", "leon", CalculateMD5Hash("bob"), "euroscan"));
            users.Add(new User("srv-02", "dave", CalculateMD5Hash("koffie"), "euroscan"));
        }

        public List<User> user ()
        {
            return users;
        }

        public bool LoginResult(string server, string accountname, string ww, string username)
        {
            foreach(User u in users)
            {
                if (u.server == server)
                {
                    if (u.username == username)
                    {
                        if (u.accountname == accountname)
                        {
                            if (u.password == ww)
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }


        private string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
